package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luo.dao.TravelGroupDao;
import com.luo.dao.TravelGroupItemDao;
import com.luo.entity.QueryPageBean;
import com.luo.pojo.TravelGroup;
import com.luo.service.TravelGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Zoro
 * @date 2021年08月25
 */
@Service(interfaceClass = TravelGroupService.class)
@Transactional(readOnly = true)
public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupDao travelGroupDao;

    @Autowired
    private TravelGroupItemDao travelGroupItemDao;

    @Override
    public PageInfo<TravelGroup> pageSelect(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<TravelGroup> travelGroups = travelGroupDao.pageSelect(queryPageBean.getQueryString());

        return new PageInfo<>(travelGroups);
    }

    @Override
    @Transactional(readOnly = false)
    public void travelGroupAdd(Integer[] travelItemIds, TravelGroup travelGroup) {
        //添加跟团行
        travelGroupDao.travelGroupAdd(travelGroup);
        //添加跟团行关联的自由行
        groupItemAdd(travelItemIds, travelGroup.getId());
    }

    @Override
    public TravelGroup getTravelGroupById(Integer travelGroupId) {
        return travelGroupDao.getTravelGroupById(travelGroupId);
    }

    @Override
    @Transactional(readOnly = false)
    public void travelGroupEdit(Integer[] travelItemIds, TravelGroup travelGroup) {
        //修改数据
        travelGroupDao.travelGroupEdit(travelGroup);

        //先删除对应的所有数据
        travelGroupItemDao.deleteAllById(travelGroup.getId());
        //再添加全部数据
        groupItemAdd(travelItemIds, travelGroup.getId());

    }

    @Override
    @Transactional(readOnly = false)
    public void travelGroupDeleteById(Integer travelGroupId) {
        //先删除对应表联系
        travelGroupItemDao.deleteAllById(travelGroupId);
        //再根据id删除跟团游信息
        travelGroupDao.travelGroupDeleteById(travelGroupId);

    }

    @Override
    public List<TravelGroup> selectAll() {
        return travelGroupDao.selectAll();
    }


    @Transactional(readOnly = false)
    public void groupItemAdd(Integer[] integers, Integer id) {
        //多条数据插入
        if (integers.length > 0) {
            travelGroupItemDao.addList(integers, id);
        }
    }
}
